package org.gnf.umls;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class Utils {

	public static Set<String> readTermFile(String termfile){
		Set<String> terms = new HashSet<String>();
		try {
			BufferedReader f = new BufferedReader(new FileReader(termfile));
			String line = f.readLine().trim();
			while(line!=null){
				String term = line;
				terms.add(term);
				line = f.readLine();
				if(term!=null)term.trim();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return terms;
	}

	public static HashMap<String, Set<String>> readMapFile(String file, String delimiter, String skipprefix, boolean reverse){
		HashMap<String,Set<String>> map = new HashMap<String, Set<String>>();
		try {
			BufferedReader f = new BufferedReader(new FileReader(file));
			String line = f.readLine().trim();
			while(line!=null){
				if(!line.startsWith(skipprefix)){
					String[] item = line.split(delimiter);
					if(item!=null&&item.length>1){
						if(reverse){
							Set<String> mapped = map.get(item[1]);
							if(mapped == null){
								mapped = new HashSet<String>();
								map.put(item[1], mapped);
							}
							mapped.add(item[0]);
						}else{
							Set<String> mapped = map.get(item[0]);
							if(mapped == null){
								mapped = new HashSet<String>();
								map.put(item[0], mapped);
							}
							mapped.add(item[1]);
						}
					}
				}
				line = f.readLine();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}
	
	
}
